package mumu.android.lib.tool;

import org.json.JSONException;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 地址： https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-
 * Deserializing-Gener
 */
public class JsonHp {

	/**
	 * json数据反序列化
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T Deserializate(String jsonStr, T tclass) throws JSONException {
		Gson gson = new Gson();
		return (T) gson.fromJson(jsonStr, tclass.getClass());
	}

	/**
	 * json数据反序列化(泛型)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T Deserializate(String jsonStr, TypeToken<T> typeToken) throws JSONException {
		Gson gson = new Gson();
		return (T) gson.fromJson(jsonStr, typeToken.getType());
	}

	/**
	 * json数据序列化
	 * 
	 */
	public static <T> String Serialization(T tclass) throws JSONException {
		Gson gson = new Gson();
		String json = gson.toJson(tclass);
		return json;
	}

	
}
